package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.TCloudRole;
import model.TCloudSubject;
import model.TTeacherInfo;
import model.VBackAdminUser;
import model.VCloudSchoolRole;
import model.VCloudTeachRole;
import model.VTeacherInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.office.NewReadExcel;
import util.office.WriteExcel;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 教师信息管理请求处理类
 * @author djf
 * @since 2018/10/6
 */
@Controller
@RequestMapping(value="/systeachermanagement")
public class SysTeacherManagementController {

	/**
	 * 添加教师信息控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO  添加教师信息控制类
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个教师信息", 
			operationClassPath = "controller.SysTeacherManagementController.addTeacher(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息添加")
	@RequestMapping(value="/addteacher")
	public String addTeacher(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		TTeacherInfo teacher=new TTeacherInfo();
		
		String schoolid=req.getParameter("schoolid");
		String realName=req.getParameter("realName");
		String sex=req.getParameter("sex");
		String birthday=req.getParameter("birthday");
		String Email=req.getParameter("Email");
		String phone=req.getParameter("phone");
		String tell=req.getParameter("tell");
		String workTel=req.getParameter("workTel");
		String address=req.getParameter("address");
		String post=req.getParameter("post");
		String graduateSchool=req.getParameter("graduateSchool");
		String entryTime=req.getParameter("entryTime");
		String roleid=req.getParameter("roleid");
		
		try {
			if(realName!=null && !realName.equals("") &&  sex!=null && !sex.equals("")  
					&&  Email!=null && !Email.equals("")  &&  phone!=null && !phone.equals("")  
					&&  roleid!=null && !roleid.equals("") && schoolid!=null && !schoolid.equals("")){
				teacher.setRealName(realName);
				teacher.setSex(sex);
				teacher.setBirthday(birthday);
				teacher.setEmail(Email);
				teacher.setPhone(phone);
				teacher.setTell(tell);
				teacher.setWorkTel(workTel);
				teacher.setAddress(address);
				teacher.setPost(post);
				teacher.setGraduateSchool(graduateSchool);
				teacher.setEntryTime(entryTime);
				teacher.setActiveState(true);
				teacher.setRoleid(Integer.parseInt(roleid));
				teacher.setSchoolid(schoolid);
				
				int row=DAOFactory.getTeacherInfoDAO().addTecher(teacher);
				if(row>0){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "教师添加成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "教师添加失败！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "教师添加失败！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code",ResponseJSON.FLAG_FAIL);
			result.put("msg", "教师添加失败！");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 教师信息修改控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 教师信息修改控制类
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "修改一个教师信息", 
			operationClassPath = "controller.SysTeacherManagementController.updateTeacher(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息查看")
	@RequestMapping(value="/updateteacher")
	public String updateTeacher(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		String teainfoid=req.getParameter("teainfoid");
		String realName=req.getParameter("realName");
		String sex=req.getParameter("sex");
		String birthday=req.getParameter("birthday");
		String Email=req.getParameter("Email");
		String phone=req.getParameter("phone");
		String tell=req.getParameter("tell");
		String workTel=req.getParameter("workTel");
		String address=req.getParameter("address");
		String post=req.getParameter("post");
		String graduateSchool=req.getParameter("graduateSchool");
		String entryTime=req.getParameter("entryTime");
		
		try {
			if(teainfoid!=null && !teainfoid.equals("") && realName != null && 
					!realName.equals("") &&  sex != null && !sex.equals("")  
					&&  Email != null && !Email.equals("")  &&  phone != null && !phone.equals("")){
				TTeacherInfo teacher=DAOFactory.getTeacherInfoDAO().getTeacher(Integer.parseInt(teainfoid));
				teacher.setRealName(realName);
				teacher.setSex(sex);
				teacher.setBirthday(birthday);
				teacher.setEmail(Email);
				teacher.setPhone(phone);
				teacher.setTell(tell);
				teacher.setWorkTel(workTel);
				teacher.setAddress(address);
				teacher.setPost(post);
				teacher.setGraduateSchool(graduateSchool);
				teacher.setEntryTime(entryTime);
				
				boolean row=DAOFactory.getTeacherInfoDAO().updateTeacher(teacher);
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "教师信息修改成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "教师信息修改失败！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空教师信息修改失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "教师信息修改失败！");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 根据查询条件得到一个没有分页的教师列表控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO  根据查询条件得到一个没有分页的教师列表控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据查询条件得到一个没有分页的教师列表", 
			operationClassPath = "controller.SysTeacherManagementController.getteacherByWhere(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息查看")
	@RequestMapping(value="/getteacherbywhere")
	public String getteacherByWhere(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		
		String userName=req.getParameter("userName");
		
		try {
			exp.andEqu("activeState", true, Boolean.class);
			if(userName!=null && !userName.equals("")){
				exp.andLeftBraLike("userName", userName, String.class);
				exp.orRightBraLike("realName", userName, String.class);
			}
			String strWhere = exp.toString();
			List<VTeacherInfo> list=DAOFactory.getTeacherInfoDAO().getTeaListBywhere(strWhere);
			if(list!=null && list.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "无查询结果");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "无查询结果");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 根据查询条件得到分页的教师信息控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 根据查询条件得到分页的教师信息控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据查询条件得到分页的教师信息", 
			operationClassPath = "controller.SysTeacherManagementController.getTeacherByPage(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息查看")
	@RequestMapping(value="/getteacherbypage")
	public String getTeacherByPage(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		
		int currentPage = 1;
		int pageSize = 10;

		try {
			String schoolid=req.getParameter("schoolid");
			String roleid = req.getParameter("roleid");
			String userName = req.getParameter("userName");
			String page = req.getParameter("page");
			String limit = req.getParameter("limit");	
			
			if (page != null && !page.equals("") && limit != null && !limit.equals("")) {				
				currentPage = Integer.parseInt(page);
				pageSize = Integer.parseInt(limit);				
			}	
			if (roleid != null && !roleid.equals("") && !roleid.equals("0")) {
				exp.andEqu("roleid", Integer.parseInt(roleid), Integer.class);
			}
			if(schoolid!=null && !schoolid.equals("")){
				exp.andEqu("schoolid", schoolid, String.class);
			}
			if(userName!=null && !userName.equals("")){
				exp.andLeftBraLike("userName", userName, String.class);
				exp.orRightBraLike("realName", userName, String.class);
			}
			exp.orderByDesc("schoolName");
			exp.orderByDesc("realName");
			String strWhere = exp.toString();
			List<VTeacherInfo> list=DAOFactory.getTeacherInfoDAO().getTeaListByPage(strWhere, currentPage, pageSize);
			List<VTeacherInfo> listsize=DAOFactory.getTeacherInfoDAO().getTeaListBywhere(strWhere);
			if(list!=null && listsize.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "无查询结果");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "发生错误，无查询结果");
		}
		
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 获得所有教师职务控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获得所有教师职务控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "获得所有教师职务", 
			operationClassPath = "controller.SysTeacherManagementController.getCloudSubject(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息查看")
	@RequestMapping(value="/getcloudsubject")
	public String getCloudSubject(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		try {
			List<TCloudSubject> list=DAOFactory.getTeacherInfoDAO().getAllCloudSubject();
			if(list!=null && list.size()>0){
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "查询成功");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "无查询结果");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "无查询结果");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 添加教师班级控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 添加教师班级控制类（无页面调用）
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "给教师设置一个班级（任课）", 
			operationClassPath = "controller.SysTeacherManagementController.addTeacherRole(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "无页面调用")
	@RequestMapping(value="/addteacherrole")
	public String addTeacherRole(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		String roleid=req.getParameter("roleid");
		String teaid=req.getParameter("teaid");
		String subid=req.getParameter("subid");
		String classid=req.getParameter("classid");
		
		try {
			int row=DAOFactory.getTeacherInfoDAO().addTeacherRole(Integer.parseInt(roleid),Integer.parseInt(teaid)
					, Integer.parseInt(subid), classid);
			if(row==1){
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "教师班级设置成功！");
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "教师班级设置失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "教师班级设置失败！");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 删除一个教师班级控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 删除一个教师班级控制类（无页面调用）
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "取消教师任课班级", 
			operationClassPath = "controller.SysTeacherManagementController.deleteTeacherRole(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "无页面调用")
	@RequestMapping(value="/deltearole")
	public String deleteTeacherRole(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		String teachid=req.getParameter("teachid");
		
		try {
			int row=DAOFactory.getTeacherInfoDAO().deleteTeacherRole(Integer.parseInt(teachid));
			if(row==1){
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "教师班级删除成功！");
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "教师班级删除失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "教师班级删除失败！");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
		
	}
	
	/**
	 * 查看教师任课班级记录控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 查看教师任课班级记录控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查看教师任课班级记录", 
			operationClassPath = "controller.SysTeacherManagementController.getTeacherRoleByPage(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "无页面调用")
	@RequestMapping(value="/getteacherrole")
	public String getTeacherRoleByPage(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		//HttpSession session = req.getSession();
		//VCloudUser user = (VCloudUser) session.getAttribute("cloudUser");
		
		int currentPage = 1;
		int pageSize = 10;
		
		String userName=req.getParameter("userName");
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
		String roleid = req.getParameter("roleid");
		
		try {
			//exp.andEqu("schoolid", user.getSchoolid(), String.class);
			
			if (roleid != null && !roleid.equals("") && !roleid.equals("0")) {
				exp.andEqu("roleid", Integer.parseInt(roleid), Integer.class);
			}
			
			if(userName!=null && !userName.equals("")){
				exp.andLeftBraLike("userName", userName, String.class);
				exp.orRightBraLike("realName", userName, String.class);
			}
			
			if (page != null && !page.equals("") && limit != null && !limit.equals("")) {
				currentPage = Integer.parseInt(page);
				pageSize = Integer.parseInt(limit);
			}
			
			String strWhere = exp.toString();
			
			List<VCloudTeachRole> list = DAOFactory.getTeacherInfoDAO().getTeachRoleByPage(strWhere, currentPage, pageSize);
			List<VCloudTeachRole> listsize = DAOFactory.getTeacherInfoDAO().getTeacherRole(strWhere);
			if(list!=null && listsize.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "无查询结果");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "无查询结果");
		}
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 得到某个学校的全部教师角色类型
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 得到某个学校的全部教师角色类型
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "得到某个学校的全部教师角色类型", 
			operationClassPath = "controller.SysTeacherManagementController.getSchoolRole(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息添加")
	@RequestMapping(value="/getschoolreole")
	public String getSchoolRole(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		//HttpSession session = req.getSession();
		//VCloudUser user = (VCloudUser) session.getAttribute("cloudUser");
		
		try {
			//String schoolid = user.getSchoolid();
//			if(schoolid!=null && !schoolid.equals("")){
//				exp.andEqu("schoolid", schoolid, String.class);
//			}
			String strwhere=exp.toString();
			List<VCloudSchoolRole> list=DAOFactory.getTeacherInfoDAO().getSchoolRole(strwhere);
			if(list!=null && list.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "无查询结果");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "无查询结果");
		}
		
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	
	
	
	
	/**
	 * Excel数据导入到数据库(2018-12-11 徐震霖)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO Excel数据导入到数据库(2018-12-11 徐震霖)
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "Excel数据导入到数据库", 
			operationClassPath = "controller.SysTeacherManagementController.saveteaList(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息添加/批量添加")
	@RequestMapping(value="/savetealist")
	public String saveteaList(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		List<TTeacherInfo> failList = new ArrayList<TTeacherInfo>();

		try {
			List<TTeacherInfo> teaList = (List<TTeacherInfo>) session.getAttribute("teaList");
			failList = DAOFactory.getTeacherInfoDAO().batchImport(teaList);
			if (failList != null && failList.size() > 0) {
				result.put("code", 1);
				result.put("msg", "存在重复记录，导入失败，请删除重复记录后重试！");
//				result.put("count", failList.size());
//				JSONArray jsonArray = JSONArray.fromObject(failList);
//				result.element("data", jsonArray);
				session.setAttribute("adminteafailList", failList);
			} else {
				result.put("code", 0);
				result.put("msg", "导入成功！");
			}
		} catch (Exception e) {
			result.put("code", 1);
			result.put("msg", "存在重复记录，导入失败，请删除重复记录后重试！");
			e.printStackTrace();
		}
		
		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
		
	}
	
	/**
	 * 把Excel导入到前台table表格里(2018-12-11 徐震霖)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO  把Excel导入到前台table表格里(2018-12-11 徐震霖)
	@RequestMapping(value="/excelloadtable")
	public String excelLoadTable(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();

		try {
			List<TTeacherInfo> teaList = (List<TTeacherInfo>) session.getAttribute("teaList");
			String op = req.getParameter("op");
			if (op.equals("0")) {
				if (teaList != null && teaList.size() > 0) {
					result.put("code", 0);
					result.put("msg", "获取成功！");
					result.put("count", teaList.size());
					JSONArray jsonArray = JSONArray.fromObject(teaList);
					result.element("data", jsonArray);
				} else {
					result.put("code", 1);
					result.put("msg", "获取失败！");
				}
			} else {
				List<TTeacherInfo> failList = (List<TTeacherInfo>)session.getAttribute("adminteafailList");
				if (failList != null && failList.size() > 0) {
					result.put("code", 0);
					result.put("msg", "获取成功！");
					result.put("count", failList.size());
					JSONArray jsonArray = JSONArray.fromObject(failList);
					result.element("data", jsonArray);
					session.setAttribute("failList", failList);
				} else {
					result.put("code", 1);
					result.put("msg", "获取失败！");
				}
			}
		} catch (Exception e) {
			result.put("code", 1);
			result.put("msg", "出现异常，获取失败，请重试！");
			e.printStackTrace();
		}

		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
		
	}
	
	/**
	 * 把Excel转换为List(2018-12-11 徐震霖)
	 * @param model
	 * @param req
	 * @param resp
	 * @param file
	 * @return
	 * @throws IOException
	 */
	// TODO 把Excel转换为List(2018-12-11 徐震霖)
	@RequestMapping(value="/getexceltablelist")
	public String getExcelTableList(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		NewReadExcel nre = new NewReadExcel();	
		List<String> outerList = null;
		List<TTeacherInfo> teaList = new ArrayList<TTeacherInfo>();
		HttpSession session = req.getSession();
		
		try {
			String schoolid = req.getParameter("schoolid");
			if (schoolid != null && !schoolid.equals("")) {
				List excelList = nre.readExcel2(req, resp);
				ok:
				for (int i = 0; i < excelList.size(); i++) {
					outerList = (List<String>) excelList.get(i);
					TTeacherInfo teacherInfo = new TTeacherInfo();
					for(int j = 0; j < outerList.size(); j++){
						switch (j) {
						case 0:
							teacherInfo.setRealName(outerList.get(j).toString());
							break;
						case 1:
							teacherInfo.setSex(outerList.get(j).toString());
							break;
						case 2:
							teacherInfo.setBirthday(outerList.get(j).toString().replace('/','-'));
							break;
						case 3:
							teacherInfo.setEmail(outerList.get(j).toString());
							break;
						case 4:
							if (outerList.get(j).toString() == null || outerList.get(j).toString().equals("") || 
							outerList.get(j).toString().equals("0") || outerList.get(j).toString().equals("无")) {
								break ok;
							}
							teacherInfo.setPhone(outerList.get(j).toString());
							break;
						case 5:
							teacherInfo.setTell(outerList.get(j).toString());
							break;
						case 6:
							teacherInfo.setWorkTel(outerList.get(j).toString());
							break;
						case 7:
							teacherInfo.setAddress(outerList.get(j).toString());
							break;
						case 8:
							teacherInfo.setPost(outerList.get(j).toString());
							break;
						case 9:
							teacherInfo.setGraduateSchool(outerList.get(j).toString());
							break;
						case 10:
							teacherInfo.setEntryTime(outerList.get(j).toString().replace('/','-'));
							break;
						case 11:
							teacherInfo.setRoleid(Integer.parseInt(outerList.get(j).toString()));
							break;
						}											
					}
					teacherInfo.setSchoolid(schoolid);
					System.out.println(teacherInfo.getRealName());
					teaList.add(teacherInfo);
				}
				if (teaList != null && teaList.size() > 0) {
					result.put("code", 0);
					result.put("msg", "获取成功！");
					result.put("count", teaList.size());
					JSONArray jsonArray = JSONArray.fromObject(teaList);
					result.element("data", jsonArray);
					session.setAttribute("teaList", teaList);
				} else {
					result.put("code", 1);
					result.put("msg", "获取失败！");
				}
			}
			else {
				result.put("code", 1);
				result.put("msg", "获取失败，学校编号不能为空！");
			}
		} catch (Exception e) {
			result.put("code", 1);
			result.put("msg", "出现异常，获取失败，请重试！");
			e.printStackTrace();
		}

		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 教师模板下载(2018-12-11 徐震霖)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 教师模板下载(2018-12-11 徐震霖)
	@RequestMapping(value="/downloadteacher")
	public String downloadTeacher(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression ex = new Expression();//查询条件拼接工具
		WriteExcel we = new WriteExcel();
		HttpSession session = req.getSession();
		
		try {		
			//后期从session里获取用户信息
			VBackAdminUser user = (VBackAdminUser) session.getAttribute("backUser");
			
			String schoolid = req.getParameter("schoolid");
			if (schoolid != null && !schoolid.equals("")) {
				ex.andEqu("schoolid", schoolid, String.class);
				ex.andEqu("parentid", Constant.TEACHER, Integer.class);
				List<TCloudRole> roleList = DAOFactory.getCloudRoleDAO().getRoleListByStrWhere(ex.toString());
				JSONArray jsonArray = JSONArray.fromObject(roleList);
				String path = we.teacherexportExcel2003("教师信息表模板", jsonArray, "后台管理员下载", user.getUserid().trim());
				if (path != null && !path.equals("")) {
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "获取成功！");
					result.put("path", path);
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "模板下载失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "要下载的学校编号不能为空！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "模板下载失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 删除教师信息控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 * @since 2018/12/9
	 */
	//TODO 删除教师信息控制类
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一个教师信息", 
			operationClassPath = "controller.SysTeacherManagementController.deleteTeacher(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息查看")
	@RequestMapping(value="/deleteteacher")
	public String deleteTeacher(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		
		try {
			String teaid = req.getParameter("teainfoid");
			if (teaid != null && !teaid.equals("")) {
				if(DAOFactory.getTeacherInfoDAO().deleteTeacher(Integer.parseInt(teaid))){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "删除成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "删除失败，存在外键引用，请删除引用后重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "教师id不能为空，删除失败，请重试！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "删除失败，存在外键引用，请删除引用后重试！");
			e.printStackTrace();
		}
		
		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
		
	}
	
}
